package com.example.tokendemo.service;

import com.example.tokendemo.common.constant.Constants;
import com.example.tokendemo.model.LoginUser;
import com.example.tokendemo.model.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import io.jsonwebtoken.Claims;
import org.springframework.util.StringUtils;

@Component
public class TokenService {
    private Key secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);

    @Value("${token.header}")
    private String header;

    @Value("${token.secret}")
    private String secret;

    @Value("${token.expireTime}")
    private int expireTime;

    protected static final long MILLIS_SECOND = 1000;

    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;

    protected static final long MILLIS_MINUTE_TEN = 20 * 60 * 1000;

    public LoginUser getLoginUser(HttpServletRequest request){
        String token = getToken(request);
        if (StringUtils.hasText(token)){
            try{
                Claims claims = parseToken(token);

                String uuid = (String)claims.get(Constants.LOGIN_USER_KEY);
                String userKey = getTokenKey(uuid);
                User user ;

                return null;
            }
            catch (Exception e){

            }
        }

        return null;
    }

    public void setLoginUser(){

    }

    public void delLoginUser(){

    }

    public String createToken(LoginUser user){
        Map<String,Object> claims = new HashMap<>();
        claims.put(Constants.LOGIN_USER_KEY, UUID.randomUUID());
        return createToken(claims);
    }

    private String createToken(Map<String, Object> claims){
        String token = Jwts.builder()
                .setClaims(claims)
                .signWith(secretKey).compact();
        return token;
    }

    public void verifyToken(){

    }

    public void refreshToken(){

    }

    public void setUserAgent(){

    }

    /**
     * get data claims from token
     *
     * @param token
     * @return
     */
    private Claims parseToken(String token){
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }

    public String getUsernameFromToken(String token){
        Claims claims = parseToken(token);
        return claims.getSubject();
    }
    
    private String getToken(HttpServletRequest request){
        String token = request.getHeader(header);
        if (!StringUtils.hasText(token) && token.startsWith(Constants.TOKEN_PREFIX))
        {
            token = token.replace(Constants.TOKEN_PREFIX, "");
        }
        return token;
    }

    private String getTokenKey(String uuid)
    {
        return Constants.LOGIN_TOKEN_KEY + uuid;
    }

}
